package org.redirect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.util.AjaxResult;
import org.redirect.mapper.InviteMapper;
import org.redirect.service.IInviteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, InviteParam> implements IInviteService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private InviteMapper inviteMapper;

    @Value("${redirect.url}")
    private String url;

    @Override
    public AjaxResult verifyUrl(InviteParam invite) {
        if (invite == null){
            return AjaxResult.error("无效链接",2010);
        }else {
            InviteParam inviteCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", invite.getInvitedCode()));
            Boolean aBoolean = redisTemplate.hasKey("share:" + invite.getInvitedCode());
            if (inviteCode != null && Boolean.TRUE.equals(aBoolean)){
                return AjaxResult.success(url);
            }
        }
        return AjaxResult.error("无效链接",2010);
    }


    @Override
    public AjaxResult verifyUrl2(InviteParam inviteCode) {

        return null;
    }
}
